package com.training.bt.controller;

import com.training.bt.common.ApiResponse;
import com.training.bt.entity.User;
import com.training.bt.exception.FriendException;
import com.training.bt.service.IFriendService;
import com.training.bt.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/friends")
@RequiredArgsConstructor
public class FriendController {

    private final IFriendService friendService;

    // 添加好友
    @PostMapping("/{friendId}")
    public ResponseEntity<ApiResponse> addFriend(@PathVariable Long friendId) {
        try {
            User currentUser = SecurityUtils.getCurrentUser();
            Long currentUserId = currentUser.getId();
            
            friendService.addFriend(currentUserId, friendId);
            return ResponseEntity.ok(ApiResponse.success("好友添加成功"));
        } catch (FriendException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            // 记录日志
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error("添加好友失败，请稍后重试"));
        }
    }

    // 删除好友
    @DeleteMapping("/{friendId}")
    public ResponseEntity<ApiResponse> removeFriend(@PathVariable Long friendId) {
        try {
            User currentUser = SecurityUtils.getCurrentUser();
            Long currentUserId = currentUser.getId();
            
            friendService.removeFriend(currentUserId, friendId);
            return ResponseEntity.ok(ApiResponse.success("好友删除成功"));
        } catch (FriendException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            // 记录日志
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error("删除好友失败，请稍后重试"));
        }
    }

    // 获取好友列表
    @GetMapping("")
    public ResponseEntity<ApiResponse> getFriends() {
        try {
            User currentUser = SecurityUtils.getCurrentUser();
            Long currentUserId = currentUser.getId();
            
            List<User> friends = friendService.getFriends(currentUserId);
            return ResponseEntity.ok(ApiResponse.success("好友列表获取成功").addData("friends", friends));
        } catch (FriendException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            // 记录日志
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error("获取好友列表失败，请稍后重试"));
        }
    }

    // 检查是否为好友
    @GetMapping("/{friendId}/isFriend")
    public ResponseEntity<ApiResponse> isFriend(@PathVariable Long friendId) {
        try {
            User currentUser = SecurityUtils.getCurrentUser();
            Long currentUserId = currentUser.getId();
            
            boolean isFriend = friendService.isFriend(currentUserId, friendId);
            return ResponseEntity.ok(ApiResponse.success("好友关系查询成功").addData("isFriend", isFriend));
        } catch (FriendException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            // 记录日志
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error("查询好友关系失败，请稍后重试"));
        }
    }
}